/*=============================================================================
    test/dragTest.js
    Copyright (C) 2008-2009, Donald W. Griffin
    All rights reserved.
    [MIT license :: see license.txt for details]
=============================================================================*/
$module("test.dragTest");

$requires("jibits.drag");
$requires("test.base");

$namespace(test, {

DragTest : $class(
{
    ctor : function (id, logger)
    {
        this.dragMgr = new jibits.DragManager(id, 
                                {
                                    handlers : this
                                    //dragAlign : null,
                                });
        this.id = id;
        this.logger = logger;

        this.ticket = jibits.eventMgr().listen(id, "click",
                                               this.onClick.bind(this));
    },

    log : function (msg)
    {
        var log = $elem(this.logger);
        var s = zjs.isEmpty(log.value) ? "" : (log.value + "\n");
        s += msg;
        log.value = s;
    },

    logDrag : function (drag)
    {
        var s = drag.type.name + "(" + drag.ev.type +
                    "): dx=" + drag.dx + " dy="+drag.dy + " x="+drag.x+" y="+drag.y;
        this.log(s);
    },

    onClick : function (ev)
    {
        this.log("CLICK: x="+ev.pageX+" y="+ev.pageY);
        ev.consume();
    },

    onDragBegin : function (drag)
    {
        this.logDrag(drag);
        this.firstMove = true;

        var dragObj = jibits.cloneElemDeep(this.id, { baseid: "dragTest" });
        jibits.setOpacity(dragObj, 0.5);
        return dragObj;
    },

    onDragMove : function (drag)
    {
        if (this.firstMove)
            this.logDrag(drag);
        this.firstMove = false;
    },

    onDragEnd : function (drag)
    {
        this.logDrag(drag);
        var x = parseInt(jibits.getStyle(this.id, "left"));
        var y = parseInt(jibits.getStyle(this.id, "top"));
        x += drag.dx;
        y += drag.dy;
        jibits.setStyle(this.id, { left : x+"px", top : y+"px" });
    }
}),

initDrag : function (id, logger)
{
    test.dragger = new test.DragTest(id, logger);
}

}) // namespace test
